//
// Created by Cedar on 2022/3/15.
//

#include <iostream>
#include <vector>
using namespace std;

class Solution {
public:
    int searchInsert(vector<int>& nums, int target) {
        // 暴力破解
//        for (int i = 0; i < nums.size(); i++) {
//            if (target <= nums[i]){
//                return i;
//            }
//        }
//        return nums.size();

        // 二分法
        int left = 0;
        int right = nums.size() - 1;
        while (left <= right){
            int mid = left + (right - left) / 2;
            if (nums[mid] == target){
                return mid;
            } else if (nums[mid] < target){
                left = mid + 1;
            } else if (nums[mid] > target) {
                right = mid - 1;
            }
        }
        return left;
    }
};

int main(){
    vector <int> nums = {2,3,5,7};
    int target = 4;
    Solution s;
    cout << s.searchInsert(nums,target) << endl;
}